package com.hduong.springcloud.controller;

import com.hduong.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * Created by dudu on 2020/3/18.
 */
@RestController
@Slf4j
public class HystrixOrderController {

    @Value("${server.port}")
    private String serverPort;

    @Resource
    private PaymentService paymentService;

    @GetMapping("/payment/ok/{id}")
    public String payment_ok(@PathVariable("id") Integer id) {
        //这个逻辑的时间是用不上的， 因为是线程私有的， 他没有共享资源， 不会阻塞， 不会在自己的代码内部出现问题
        //只是可能抢不到线程资源
        long start = System.currentTimeMillis();
        String result = paymentService.paymentInfo_Ok(id);
        log.info("**********result: " + result);
        long end = System.currentTimeMillis();

        return result + "花费:" +  (end - start)   + "豪秒";
    }

    @GetMapping("/payment/timeout/{id}")
    public String timerout(@PathVariable("id") Integer number) {
        long start = System.currentTimeMillis();
        String result = paymentService.timeout(number);
        log.info("**********result: " + result);
        long end = System.currentTimeMillis();
        return result + "花费:" +  (end - start)   + "豪秒";
    }

    @GetMapping("/payment/hystrix/ok/{id}")
    public String paymentInfo_OK(@PathVariable("id") Integer id) {
        return paymentService.paymentCircuitBreak(id);
    }

    @GetMapping("/payment/hystrix/timeout/{id}")
    public String paymentInfo_Timeout(@PathVariable("id") Integer id) {
        return paymentService.payinfo_timeoutHandler(id);
    }
    @GetMapping("/payment/circuit/{id}")
    public String paymentInfo_CircuitBreak(@PathVariable("id") Integer id) {
        return paymentService.paymentCircuitBreak(id);
    }


}
